using System;
using System.Collections;
using System.Collections.Generic;
using Assets.Script;
using UnityEngine;

public class GamePanelControl
{
    GamePanelView view;
    GamePanelModel model;
    float totalSeconds ;
    bool isComeAgain = true;
    GameObject showTextObj;
    public GamePanelControl(GamePanelView gamePanelView)
    {
        view=gamePanelView;
        model=new GamePanelModel("GamePanelModelName"); 
        MessageCenter.Ins.ListenMess<Dictionary<string,PlayerData>>(MessId.RefureRankingList,RefureRankingList);
        MessageCenter.Ins.ListenMess(MessId.OpenLextObj, OpenLextObj);
        MessageCenter.Ins.ListenMess(MessId.CloseLextObj, CloseLextObj);
        MessageCenter.Ins.ListenMess<int>(MessId.GameOver, GameOver);
        BaindEvent();
    }
    public void OpenLextObj()
    {
        if (showTextObj == null)
        {
            showTextObj = GameObject.Find("Canvas/LextObj");
        }
        showTextObj.SetActive(true);
    }
    public void CloseLextObj()
    {
        if (showTextObj == null)
        {
            showTextObj = GameObject.Find("Canvas/LextObj");
        }
        showTextObj.SetActive(false);
    }

    private void GameOver(int obj)
    {
        switch ((GameOverType)obj)
        {
            case GameOverType.enemy:
                {
                    if (isComeAgain)
                    {
                        isComeAgain = false;
                        UIManager.Ins.OpenUI(UIName.GameOverOnePanel);
                        CloseLextObj();
                    }
                    else
                    {
                        isComeAgain = true;
                        UIManager.Ins.CloseUI(UIName.GamePanel);
                        UIManager.Ins.OpenUI(UIName.GameOverTwoPanel);
                        CloseLextObj();
                    }
                }
                break;
            case GameOverType.time:
                {

                }
                break;
            case GameOverType.boss:
                {

                }
                break;
            default:
                break;
        }
    }

    public void Open(){
        MainPanelModel models=ModelManager.Ins.GetModel(MainPanelModel.m_name) as MainPanelModel;
        ModelManager.Ins.AddModel(model);
        PlayerData data=models.GetPlayerData(); 
        data.name="player";
        isComeAgain = true;
        model.RefurePlayerData(data);
        models.RefurePlayerData(new PlayerData(){
            nowLevel=data.nowLevel,
            name="player"
        });
        totalSeconds = model.GetCountDownTimeNum();
    }
    public void BaindEvent(){

    }
    public void RefureRankingList(Dictionary<string,PlayerData> dic){
        List<PlayerData> list=new List<PlayerData>();
        foreach (var item in dic){
            list.Add(item.Value);
        }
        list.Sort((a, b) =>
        {
            if (a.nowLevel > b.nowLevel)
            {
                return -1;
            } else if (a.nowLevel < b.nowLevel)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        });
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].name == "player")
            {
                PlayerPrefs.SetInt("PlayerTH", i + 1);
                PlayerPrefs.SetInt("PlayerLevel", list[i].nowLevel);
            }
        }
        for(int i=0;i<view.RankingListObjs.Count-1;i++)
        {
            if (i < list.Count)
            {
                view.RankingListObjs[i].GetComponent<RankingListIcon>().SetName(list[i].name);
                view.RankingListObjs[i].GetComponent<RankingListIcon>().SetLevel(list[i].nowLevel);
            }
            else
            {
                view.RankingListObjs[i].GetComponent<RankingListIcon>().SetName("");
                view.RankingListObjs[i].GetComponent<RankingListIcon>().SetLevel(0);
            }
        }
        for (int i=3;i<list.Count;i++){
            if(list[i].name=="player"){
                view.RankingListObjs[3].transform.localScale = Vector3.one;
                view.RankingListObjs[3].GetComponent<RankingListIcon>().SetName(list[i].name);
                view.RankingListObjs[3].GetComponent<RankingListIcon>().SetLevel(list[i].nowLevel); 
                view.RankingListObjs[3].GetComponent<RankingListIcon>().SetNum(i+1);
                return;
            }else{
                view.RankingListObjs[3].transform.localScale = Vector3.zero;
            }
        }
    }
    public void Update()
    {
        if (totalSeconds >= 0)
        {
            totalSeconds -= Time.deltaTime;
            TimeSpan timeSpan = TimeSpan.FromSeconds(MathF.Ceiling(totalSeconds));
            view.countDownTimeText.text = $"{(int)timeSpan.TotalMinutes:D2}:{timeSpan.Seconds:D2}";

            if ( totalSeconds < 0)
            {
                GameOver(1);
            }
        }
    }

    internal void Close()
    {
        model.ClaerData();
    }
}
